﻿using System;
using Com.Hikvision.Wrapper.Core;

namespace Com.Hikvision.Wrapper.Service.EntranceControl.RemoteEntranceControl
{
    public class LoginManage
    {
        private static readonly Core.Infrastructure coreInfrastructure = new Infrastructure();

        /// <summary>
        /// 登录设备
        /// </summary>
        /// <param name="deviceIp"></param>
        /// <param name="devicePort"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        public int LoginDevice(string deviceIp, int devicePort, string userName, string password, out int errorCode)
        {
            errorCode = 0;
            try
            {
                var struDeviceInfoV40 = new CHCNetSDK.NET_DVR_DEVICEINFO_V40();

                var iUserId =
                    coreInfrastructure.NetDvrLoginV40(deviceIp, devicePort, userName, password, ref struDeviceInfoV40);

                if (iUserId >= 0) return iUserId;

                errorCode = (int) coreInfrastructure.NetDvrGetLastError();
                coreInfrastructure.NetDvrCleanUp();
                return -1;
            }
            catch
            {
                errorCode = -1;
                return 0;
            }

        }
    }
}